--[[
@module mapcmd
]]
local lume = require (LIB_PATH .. "lume")

local output_map_list = function()
	local list = Sled.maps
	return lume.reduce(lume.map(list, function (v)
			local name = v.name
			if Sled.current_map == v then
				name = "*"..name
			end
			return name
	end),
	
	function(v, w)
		return string.format("%s %s", v, w)
	end)
end
local Command = require "command"

return Command("map", {

	{
		"new", function (name, w, h)
			if not name then return "no name given" end
			local w, h = tonumber(w), tonumber(h)
			Sled:make_map(name, w, h)
			local w, h = Sled.current_map:get_dimensions()
			return string.format("new map %s (%i x %i)", name, w, h)
		end
	},
	
	{
		"list", function ()
		if #Sled.maps == 0 then return 'no map' end
			return output_map_list ()
		end,
	},
	
	{
		"switch", function (name)
			if #Sled.maps == 0 then return 'no map' end
			Sled.current_map = lume.match(Sled.maps, function (v)
				return v.name == name
			end) or Sled.current_map
		end
	},
	
	{
		"discard", function (name)
			if #Sled.maps == 0 then return 'no map' end
			local name = name or Sled.current_map.name
			local m = Sled:get_map(name)
			if not m then return name .. ' not found' end
			lume.remove(Sled.maps, m)
			Sled.current_map = nil
			Sled.current_layer = nil
			Sled.current_tile = nil
			return m.name .. ' removed'
		end,
	},
	{
		"rename", function (str)
			if not str then return "bad input" end
			Sled.current_map:rename(str)
		end,
	}
})
